package com.jiulan.hearthearing.image;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.jiulan.hearthearing.constants.UrlConstants;
import com.shehuan.niv.NiceImageView;

import java.io.File;

public class ImageRequest {
    private Context context;

    public ImageRequest(Context context) {
        this.context = context;
    }

    public String format(String imgurl) {
        if (imgurl.startsWith("http://") || imgurl.startsWith("https://")) {
            return imgurl;
        } else if (imgurl.startsWith("/")) {
            return UrlConstants.Base_Http + imgurl;
        } else {
            return UrlConstants.Base_Http + "/" + imgurl;
        }
    }

    /**
     * 标准网络请求
     *
     * @param url
     * @param imageView
     */
    private void get(String url, ImageView imageView) {
        RequestOptions options = new RequestOptions();
//        options.placeholder(R.mipmap.dengdai);
        options.skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        Glide.with(context).load(format(url)).apply(options).into(imageView);
    }

    /**
     * 300x压缩请求
     *
     * @param url
     * @param imageView
     */
    @SuppressLint("CheckResult")
    private void get300x(String url, ImageView imageView) {
        RequestOptions options = new RequestOptions();
//        options.placeholder(R.mipmap.dengdai);
        options.skipMemoryCache(true)
                .override(300, 300)
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        Glide.with(context).load(format(url)).apply(options).into(imageView);
    }


    /**
     * 资源文件夹（drawable mipmap）图片加载
     *
     * @Context 传递的上下文
     * @Integer 资源文件（drawable mipmap）对象
     * @ImageView ImageView对象
     */
    public void getDrawable(Context context, Integer resourceId, ImageView imageView) {

        RequestOptions options = new RequestOptions();
//        options.placeholder(R.mipmap.dengdai);
        options
//                .transform(new GlideCircleTransform(context))
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        Glide.with(context).load(resourceId).apply(options).into(imageView);
    }

    /**
     * 资源文件夹 圆形 图片加载
     *
     * @Context 传递的上下文
     * @Integer 资源文件（drawable mipmap）对象
     * @ImageView ImageView对象
     */
    public void getDrawableCircle(Context context, Integer resourceId, NiceImageView imageView) {
        Glide.with(context).load(resourceId).into(imageView);
    }


    /**
     * 本地文件路径图片加载
     *
     * @Context 传递的上下文
     * @File 本地文件路径
     * @ImageView ImageView对象
     */
    public void getFile(Context context, File file, ImageView imageView) {
        Glide.with(context).load(file).into(imageView);
    }


    /**
     * Gif动图加载 资源文件
     * * @context       传递的上下文
     *
     * @url 网络地址
     * @imageViewurl ImageView对象
     * @number 0为无限循环其他数未正常循环次数
     */
    public void getGif(Context context, int resourceId, final ImageView imageView, final int number) {

        if (number == 0) {
            RequestOptions options = new RequestOptions();
            options.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
            Glide.with(context).load(resourceId).listener(new RequestListener<Drawable>() {
                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                    return false;
                }

                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                    return false;
                }
            }).apply(options).into(imageView);

        } else {

            Glide.with(context).load(resourceId).into(new SimpleTarget<Drawable>() {
                @Override
                public void onResourceReady(Drawable drawable, Transition<? super Drawable> transition) {
                    if (drawable instanceof GifDrawable) {
                        GifDrawable gifDrawable = (GifDrawable) drawable;
                        gifDrawable.setLoopCount(number);
                        imageView.setImageDrawable(drawable);
                        gifDrawable.start();
                    }
                }
            });

        }
    }

}
